package com.edu.controller;

import com.edu.beans.Vip;
import com.edu.exception.UsedException;
import com.edu.service.VipsService;
import com.edu.service.impl.VipsServiceImpl;
import com.edu.utils.FormBeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/VipsServlet.do")
public class VipsServlet extends HttpServlet {

    VipsService vipsService = new VipsServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String action = req.getParameter("action");
        switch (action) {
            case "addVip":
                addVips(req, resp);
                break;
            case "queryVips":
                queryVips(req, resp);
                break;
            case "deleteVips":
                deleteVips(req, resp);
                break;
            case "goUpdateVip":
                goUpdateVip(req, resp);
                break;
            case "updateVip":
                updateVip(req, resp);
                break;
            default:
                resp.getWriter().write("<script>alert('未知操作');window.location.href='/views/index.jsp';</script>");
                break;
        }
    }

    /**
     * 更新会员信息
     *
     * @param req  请求对象
     * @param resp 响应对象
     */
    private void updateVip(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.getParameter("id");
        Vip vip = FormBeanUtils.populate(req, Vip.class);
        int count = vipsService.updateVip(vip);
        if (count > 0) {
            resp.getWriter().write("<script>alert('会员信息更新成功');window.location.href='/VipsServlet.do?action=queryVips';</script>");
        } else {
            resp.getWriter().write("<script>alert('会员信息更新失败');window.location.href='VipsServlet.do?action=goUpdateVip';</script>");
        }
    }

    /**
     * 跳转到更新会员页面
     *
     * @param req  请求对象
     * @param resp 响应对象
     */
    private void goUpdateVip(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Vip vip = vipsService.findVipByid(Integer.parseInt(id));
        req.setAttribute("v", vip);
        req.getRequestDispatcher("/views/vip_update.jsp").forward(req, resp);
    }

    /**
     * 删除会员
     *
     * @param req  请求对象
     * @param resp 响应对象
     */
    private void deleteVips(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            String id = req.getParameter("id");
            int count = vipsService.deleteVipById(Integer.parseInt(id));
            if (count > 0) {
                resp.getWriter().write("<script>alert('会员删除成功');window.location.href='/VipsServlet.do?action=queryVips';</script>");
            } else {
                resp.getWriter().write("<script>alert('会员删除失败');window.location.href='/VipsServlet.do?action=queryVips';</script>");
            }
        } catch (UsedException e) {
            resp.getWriter().write("<script>alert('" + e.getMessage() + "');window.location.href='/VipsServlet.do?action=queryVips';</script>");
        }
    }

    /**
     * 查询会员
     *
     * @param req  请求对象
     * @param resp 响应对象
     */
    private void queryVips(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phone = req.getParameter("phone");
        if (phone == null) {
            phone = "";
        }
        List<Vip> vipList = vipsService.findVipsByPhone(phone);

        req.setAttribute("vipList", vipList);
        req.setAttribute("phone", phone);
        req.setAttribute("msg", vipList.size() > 0 ? "" : "没有查询到此手机号");
        req.getRequestDispatcher("/views/vip_list.jsp").forward(req, resp);
    }

    /**
     * 添加会员
     *
     * @param req  请求对象
     * @param resp 响应对象
     */
    private void addVips(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            Vip vip = FormBeanUtils.populate(req, Vip.class);
            int count = vipsService.addVip(vip);
            if (count > 0) {
                resp.getWriter().write("<script>alert('会员添加成功');window.location.href='/VipsServlet.do?action=queryVips';</script>");
            } else {
                resp.getWriter().write("<script>alert('会员添加失败');window.location.href='/views/vip_add.jsp';</script>");
            }
        } catch (UsedException e) {
            String message = e.getMessage();
            resp.getWriter().write("<script>alert('" + message + "');window.location.href='/views/vip_add.jsp';</script>");
        }
    }
}
